package com.spring.web.adminmanage.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

/**
 * 日期格式化工具类
 * @author Administrator
 *
 */
public class DateFormatUtil {

	/** 日期类型年月日定义 */
	public static final int DT_YYYYMMDD = 1;
	/** 日期类型年月日时分秒定义 */
	public static final int DT_YYYYMMDD_HHMMSS = 2;
	/** 时间类型时分秒定义 */
	public static final int DT_HHMMSS = 3;
	/** 时间类型时分定义 */
	public static final int DT_HHMM = 4;
	/** 日期类型年月日时分定义 */
	public static final int DT_YYYYMMDD_HHMM = 5;
	/** 日期类型年月日定义 */
	public static final int DT_yyyymmdd = 6;

	/** 将日期格式化为yyyy-MM-dd HH:mm:ss型格 */
	public static DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	/** 将日期格式化为yyyy-MM-dd HH:mm:ss SSS型格 */
	public static DateFormat sdfFull = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss SSS");
	/** 将日类式化为yyyy-MM-dd型格 */
	public static DateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");
	/** 将日期格式化为MM类型 */
	public static DateFormat monthFormat = new SimpleDateFormat("MM");
	/** 将日期格式为dd类型 */
	public static DateFormat dayFormat = new SimpleDateFormat("dd");
	/** 将日期格式化为yyyyMMdd类型 */
	public static DateFormat format3 = new SimpleDateFormat("yyyyMMdd");
	/** 将日期格式化为MM月dd日类型 */
	public static DateFormat format4 = new SimpleDateFormat("MM月dd日");
	/** 将日期格式化为yyyy-MM-dd类型 */
	public static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
	/** 将日期格式化为yyyyMMddhhmmss类型 */
	public static SimpleDateFormat spayf = new SimpleDateFormat(
			"yyyyMMddhhmmss");
	/** 将日期格式化为yyyyMMdd类型 */
	public static DateFormat sparDate = new SimpleDateFormat("yyyyMMdd");
	/** 清空 */
	public static Calendar calendar = new GregorianCalendar();
	/** 将日期格式化为yyyy年MM月dd日类型 */
	public static SimpleDateFormat ymdh = new SimpleDateFormat("yyyy年MM月dd日");

	/**
	 * 获取当前时间 yyyyMMddHHmmss
	 * 
	 * @return
	 */
	public static String getCurrTime() {
		Date now = new Date();
		SimpleDateFormat outFormat = new SimpleDateFormat("yyyyMMddHHmmss");
		String s = outFormat.format(now);
		return s;
	}

	/**
	 * 获取当前时间 yyyyMMddHHmmss
	 *
	 * @return
	 */
	public static String getNowCurrTime() {
		Date now = new Date();
		SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String s = outFormat.format(now);
		return s;
	}

	/**
	 * 携程转换日期专用
	 * @param date
	 * @return
	 */
	public static StringBuffer fomatDateCtrip(String ctripdate){
		StringBuffer sb = new StringBuffer();
		sb.append(ctripdate);
		sb.append("T00:00:00");
		return sb;
	}
	
	
	/**
	 * 获取当前时间 yyyyMMddHHmmss
	 * 
	 * @return
	 */
	public static String getCurrDay() {
		Date now = new Date();
		SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd");
		String s = outFormat.format(now);
		return s;
	}

	/**
	 * 返回两个日期相差的天数
	 * 
	 * @param startDate 开始日期
	 * @param endDate 结束日期
	 * @return 两个日期相差的天数
	 * @throws ParseException
	 */
	public static long getDistDates(Date startDate, Date endDate) {
		Calendar aCalendar = Calendar.getInstance();
		aCalendar.setTime(startDate);
		long time0 = aCalendar.getTimeInMillis();
		aCalendar.setTime(endDate);
		long time1 = aCalendar.getTimeInMillis();
		return (time1 - time0) / (1000 * 60 * 60 * 24);
	}
	/**
	 * 获取两个时间相差的秒
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public static long getDistDatesBySecond(Date startTime, Date endTime){
		Calendar aCalendar = Calendar.getInstance();
		aCalendar.setTime(startTime);
		long time0 = aCalendar.getTimeInMillis();
		aCalendar.setTime(endTime);
		long time1 = aCalendar.getTimeInMillis();
		return (time1 - time0) / (1000);
	}

	/**
	 * 接受YYYY-MM-DD的日期字符串参数,返回两个日期相差的天数
	 * 
	 * @param start
	 * @param end
	 * @return
	 * @throws ParseException
	 */
	public static long getDistDates(String start, String end)
			throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date startDate = sdf.parse(start);
		Date endDate = sdf.parse(end);
		return getDistDates(startDate, endDate);
	}
	
	/**
	 * 日期间隔数
	 * 
	 * @param beginTime 开始时间
	 * @param endTime 结束时间
	 * @return 日期间隔数
	 */
	public static int getDiffDate(Date beginTime, Date endTime) {
		return (int) ((beginTime.getTime() - endTime.getTime()) / (24 * 3600 * 1000));
	}

	/**
	 * 获得该日期所在的星期一的日期
	 * 
	 * @param date
	 * @return
	 */
	public static Date getMondayDate(Date date) {
		if (date == null)
			date = new Date();
		Calendar aCalendar = Calendar.getInstance();
		aCalendar.setTime(date);
		aCalendar.set(Calendar.MINUTE, 0);
		aCalendar.set(Calendar.SECOND, 0);
		aCalendar.set(Calendar.HOUR, 0);
		aCalendar.set(Calendar.AM_PM, Calendar.AM);
		aCalendar.set(Calendar.MILLISECOND, 0);
		int md = aCalendar.get(Calendar.DAY_OF_WEEK);
		if (md == 1) {
			md = 8;
		}
		aCalendar.add(Calendar.DATE, -(md - 2));
		return aCalendar.getTime();
	}

	/**
	 * 格式化日期类型
	 * 
	 * @param date
	 * @param nFmt
	 * @return
	 */
	public static String formatDate(Date date, int nFmt) {
		if (date == null) {
			return "";
		}
		SimpleDateFormat dateFormat = new SimpleDateFormat();
		switch (nFmt) {
		default:
		case DateFormatUtil.DT_YYYYMMDD:
			dateFormat.applyPattern("yyyy-MM-dd");
			break;
		case DateFormatUtil.DT_YYYYMMDD_HHMMSS:
			dateFormat.applyPattern("yyyy-MM-dd HH:mm:ss");
			break;
		case DateFormatUtil.DT_HHMMSS:
			dateFormat.applyPattern("HH:mm:ss");
			break;
		case DateFormatUtil.DT_HHMM:
			dateFormat.applyPattern("HH:mm");
			break;
		case DateFormatUtil.DT_YYYYMMDD_HHMM:
			dateFormat.applyPattern("yyyy-MM-dd HH:mm");
			break;
		case DateFormatUtil.DT_yyyymmdd:
			dateFormat.applyPattern("yyyyMMdd");
			break;
		}
		return dateFormat.format(date);
	}

	/**
	 * 将日期格式化String类型
	 * 
	 * @param date
	 * @param strFmt
	 * @return
	 */
	public static String formatDate(Date date, String strFmt) {
		if (date == null) {
			return "";
		}
		SimpleDateFormat dateFormat = new SimpleDateFormat(strFmt);
		return dateFormat.format(date);
	}

	/**
	 * 格式化日期
	 * 
	 * @param strDate
	 * @param nFmt
	 * @return
	 * @throws Exception
	 */
	public static Date parseDate(String strDate, int nFmt) throws Exception {
		SimpleDateFormat dateFormat = new SimpleDateFormat();
		switch (nFmt) {
		default:
		case DateFormatUtil.DT_YYYYMMDD:
			dateFormat.applyPattern("yyyy-MM-dd");
			break;
		case DateFormatUtil.DT_YYYYMMDD_HHMMSS:
			dateFormat.applyPattern("yyyy-MM-dd HH:mm:ss");
			break;
		case DateFormatUtil.DT_HHMMSS:
			dateFormat.applyPattern("HH:mm:ss");
			break;
		case DateFormatUtil.DT_HHMM:
			dateFormat.applyPattern("HH:mm");
			break;
		case DateFormatUtil.DT_YYYYMMDD_HHMM:
			dateFormat.applyPattern("yyyy-MM-dd HH:mm");
			break;
		case DateFormatUtil.DT_yyyymmdd:
			dateFormat.applyPattern("yyyyMMdd");
			break;
		}
		return dateFormat.parse(strDate);
	}

	/**
	 * 将日期格式化为Date类型
	 * 
	 * @param strDate
	 * @param strFmt
	 * @return
	 * @throws Exception
	 */
	public static Date parseDate(String strDate, String strFmt)
			throws Exception {
		SimpleDateFormat dateFormat = new SimpleDateFormat(strFmt);
		return dateFormat.parse(strDate);
	}
	
	/**
	 * @作者 zhangzheng
	 * @时间 2011-06-19
	 * @描述 计算当前日期前七天内的开始和结束日期
	 * @param day 指定的日期
	 * @return
	 */
	@SuppressWarnings("static-access")
	public static Date[] weekStartAndEnd() {
		Date[] dates = new Date[2];
		try {
			Calendar cal = Calendar.getInstance();
			cal.add(cal.DAY_OF_YEAR,1);
			String tomorrow = DateFormatUtil.formatDate(cal.getTime(), "yyyy-MM-dd")+" 00:00:00";
			dates[1] = DateFormatUtil.parseDate(tomorrow, "yyyy-MM-dd HH:mm:ss");
			cal.add(cal.DAY_OF_YEAR, -8);
			String startDate = DateFormatUtil.formatDate(cal.getTime(), "yyyy-MM-dd")+" 23:59:59";
			dates[0] = DateFormatUtil.parseDate(startDate, "yyyy-MM-dd HH:mm:ss");
		} catch (Exception e) {
			// TODO Auto-generated catch block
		}
		return dates;
	}

	/**
	 * 指定日期
	 * 
	 * @param date 日期
	 * @return 格式化后日期
	 */
	public static Date getLastDateByMonth(Date date) {
		Calendar now = Calendar.getInstance();
		now.setTime(date);
		now.set(Calendar.MONTH, now.get(Calendar.MONTH) + 1);
		now.set(Calendar.DATE, 1);
		now.set(Calendar.DATE, now.get(Calendar.DATE) - 1);
		now.set(Calendar.HOUR, 11);
		now.set(Calendar.MINUTE, 59);
		now.set(Calendar.SECOND, 59);
		return now.getTime();
	}

	/**
	 * 日期偏差数
	 * 
	 * @param date 日期
	 * @param offset 设置
	 * @return 日期偏差数
	 */
	public static Date changeDay(Date date, int offset) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_YEAR,
				(calendar.get(Calendar.DAY_OF_YEAR) + offset));
		return calendar.getTime();
	}
	/**
	 * 将当前日期,向后追加天数
	 * @param Date
	 * @param num
	 * @return
	 */
	public static Date appendDate(Date date, int num){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, num);
		return calendar.getTime();
	}
	
	public static String appendDate(String date, int num, String format) throws Exception{
		return DateFormatUtil.formatDate(appendDate(DateFormatUtil.parseDate(date, format), num), format);
	}
	/**
	 * 获得当前月份的第一天的日期
	 * @param date
	 * @return
	 */
	public static Date getCurrMouthDateFirst(){
		Calendar curCal = Calendar.getInstance();
        curCal.set(Calendar.DATE, 1);
        return curCal.getTime();
	}
	/**
	 * 获取系统当前时间0时0分0秒，今天刚开始的时间 格式：yyyyMMddHHmmss
	 * @return
	 */
	public static String getAppendDate0000(){
		Date now = new Date();
		now.setHours(0);
		now.setMinutes(0);
		now.setSeconds(0);
		SimpleDateFormat outFormat = new SimpleDateFormat("yyyyMMddHHmmss");
		String s = outFormat.format(now);
		return s;
	}
	public static String getCurrMouthDateFirstStr(){
		return DateFormatUtil.formatDate(getCurrMouthDateFirst(), "yyyy-MM-dd");
	}
	/**
	 * 獲取上個月的第一天和最後一天的日期
	 * @return
	 */
	public static String[] getmonthminandmaxdate(){
		SimpleDateFormat fomat=new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar = Calendar.getInstance();
		Calendar calendar2 = Calendar.getInstance();
	      //得到月初
		calendar.set(Calendar.MONTH,calendar.get(Calendar.MONTH)-1);
	      calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
	      Date strDateFrom = calendar.getTime();
	      //得到月末
	      calendar2.set(Calendar.MONTH,calendar2.get(Calendar.MONTH)-1);
	      calendar2.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
	      Date strDateTo = calendar.getTime();
	      Date strDateTo2 = calendar2.getTime();
	      String time3=fomat.format(strDateTo);
	      String time4=fomat.format(strDateTo2);
		return new String[] {time3,time4};
	}
	/**
	 * 獲取當前月份的上一個月的年月
	 */
	public static String getYearMonth(){
		SimpleDateFormat fomat=new SimpleDateFormat("yyyyMM");
		Calendar calendar = Calendar.getInstance();
		//得到上一個月
		calendar.set(Calendar.MONTH,calendar.get(Calendar.MONTH)-1);
	      Date strDateFrom = calendar.getTime();
	      String time3=fomat.format(strDateFrom);
		return time3;
	}
	/**
	 * 获取当前系统时间日期
	 */
	public static String getDay() {
		SimpleDateFormat fomat=new SimpleDateFormat("dd");
		Date time = new Date();
		return fomat.format(time);
	}
	
	/**
	 * 取出一个指定长度大小的随机正整数.
	 * 
	 * @param length int 设定所取出随机数的长度。length小于11
	 * @return int 返回生成的随机数。
	 */
	public static int buildRandom(int length) {
		int num = 1;
		double random = Math.random();
		if (random < 0.1) {
			random = random + 0.1;
		}
		for (int i = 0; i < length; i++) {
			num = num * 10;
		}
		return (int) ((random * num));
	}
	
	/**
	 * 返回数字星期 星期天是0 星期一是1
	 * 
	 */
	public static int getWeekNum(Date date) {
		Calendar cal = new GregorianCalendar();
		cal.setTime(date);
		return cal.get(Calendar.DAY_OF_WEEK) - 1;
	}
	
	/**
	 * 时间前推或后推分钟,其中JJ表示分钟.
	 */
	public static Date getPreTime(Date date,Integer minute) {
		try {
			long Time = (date.getTime() / 1000) + minute * 60;
			date.setTime(Time * 1000);
		} catch (Exception e) {
		}
		return date;
	}
	
	/**
	 * 获得当前日期
	 * @param format 日期格式
	 * @return
	 */
	public static String getCurrDayByFormat(String format) {
		Date now = new Date();
		SimpleDateFormat outFormat = new SimpleDateFormat(format);
		String s = outFormat.format(now);
		return s;
	}

	/**
	 * 转换成格林威治时间
	 * @param format 若时间为"",则Date也为空
	 * @return
	 */
	public static Date formatToGMTDate(String format){
		SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
		Date startDate = null;
		try {
			startDate = format == "" ? null : sdf.parse(format);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return startDate;
	}

	public static void main(String[] args) {
		try {
			System.out.println(appendDate("2015-12-31", 1, "yyyy-MM-dd"));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
